<?php
class utility
{
    public static function randomkeys($length)
    {
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ,./&amp;lt;&gt;?;#:@~[]{}-_=+)(*&amp;^%$£"!'; //字符池
        $key = "";
        for ($i = 0; $i < $length; $i++)
        {
            $key .= $pattern{mt_rand(0, 62)}; //生成php随机数
        }
        return $key;
    }

    public static function getfirstchar($fchar)
    {

        if (ord($fchar) >= ord("A") and ord($fchar) <= ord("z")) return strtoupper($fchar{0});
        $s = iconv("UTF-8", "gb2312", $fchar);
        $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
        if ($asc >= -20319 and $asc <= -20284) return "A";
        if ($asc >= -20283 and $asc <= -19776) return "B";
        if ($asc >= -19775 and $asc <= -19219) return "C";
        if ($asc >= -19218 and $asc <= -18711) return "D";
        if ($asc >= -18710 and $asc <= -18527) return "E";
        if ($asc >= -18526 and $asc <= -18240) return "F";
        if ($asc >= -18239 and $asc <= -17923) return "G";
        if ($asc >= -17922 and $asc <= -17418) return "H";
        if ($asc >= -17417 and $asc <= -16475) return "J";
        if ($asc >= -16474 and $asc <= -16213) return "K";
        if ($asc >= -16212 and $asc <= -15641) return "L";
        if ($asc >= -15640 and $asc <= -15166) return "M";
        if ($asc >= -15165 and $asc <= -14923) return "N";
        if ($asc >= -14922 and $asc <= -14915) return "O";
        if ($asc >= -14914 and $asc <= -14631) return "P";
        if ($asc >= -14630 and $asc <= -14150) return "Q";
        if ($asc >= -14149 and $asc <= -14091) return "R";
        if ($asc >= -14090 and $asc <= -13319) return "S";
        if ($asc >= -13318 and $asc <= -12839) return "T";
        if ($asc >= -12838 and $asc <= -12557) return "W";
        if ($asc >= -12556 and $asc <= -11848) return "X";
        if ($asc >= -11847 and $asc <= -11056) return "Y";
        if ($asc >= -11055 and $asc <= -10247) return "Z";
        return "ot";
    }

    public static function uploadFile($uploadType, $fileControlName)
    {
        if ($_FILES["$fileControlName"]["error"] > 0) {

        }
        else
        {
            $folderName = date("Ymd");
            $fileName = $_FILES[$fileControlName]["name"];

            $uploadFolder = "uploads/" . $uploadType . "/" . $folderName . "/"; //.$folderName.self::randomkeys(5).substr($fileName,strrpos($fileName,".",0))
            if (is_dir($uploadFolder) == false)
                @mkdir($uploadFolder, 0777, true);
            $uploadFolder = $uploadFolder . date("YmdHis") . self::randomkeys(5) . substr($fileName, strrpos($fileName, ".", 0));
            @move_uploaded_file($_FILES[$fileControlName]["tmp_name"], $uploadFolder);
            return $uploadFolder;
        }
    }

    public static function getJavascript($scriptStr)
    {
        return "<script type=\"text/javascript\" language=\"javascript\">" . $scriptStr . "</script>";
    }

    public static function getPageNavigator($currentPageNumber, $totalPageNumber, $listPageNumber, $pageParameterName, $urlAttachment = null, $url = "")
    {
        //$url = "http://".$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

        if (strrpos($url, "&" . $pageParameterName . "=", 0) != false)
            $url = preg_replace("/&" . $pageParameterName . "=\d?/i", "", $url);
        if (strrpos($url, $pageParameterName . "=", 0) != false)
            $url = preg_replace("/" . $pageParameterName . "=\d?/i", "", $url);
        if (strrpos($url, "=", 0) != false)
            $url = $url . "&" . $pageParameterName . "=";
        else
            $url = $url . "?" . $pageParameterName . "=";
        if ($urlAttachment != "") {
            $index = strrpos($url, "?", 0);
            $temp = substr($url, 0, $index);
            $url = $urlAttachment . str_replace($temp, "", $url);
        }
        $temp = $listPageNumber;
        //        if(strrpos($url,$pageParameterName,0) != false)
        //            $url = substr($url,0,strrpos($url,$pageParameterName,0));
        //        else
        //        {
        //            $index = strrpos($url,"/",0) ;
        //            if($index != strlen($url)-1)
        //               $url = $url ."/";
        //        }
        //        if($urlAttachment != "" && strrpos($url,$urlAttachment,0) == false)
        //            $url = $url.$urlAttachment."/";
        //        $url = $url.$pageParameterName."/";

        $navigatorStr = "<div class=\"page_list\"><span"; //
        if ($currentPageNumber == 1)
            $navigatorStr = $navigatorStr . " style=\"color:#d0d0d0\">上一页";
        else
            $navigatorStr = $navigatorStr . "> " . link_to("上一页", $url . ($currentPageNumber - 1));
        //<a href=\"".$url.($currentPageNumber-1)."\">上一页</a>";
        $navigatorStr = $navigatorStr . "</span>";

        if (($currentPageNumber / $listPageNumber) < 1)
            $i = 1;
        else
        {
            $tempStr = $currentPageNumber / $listPageNumber;
            $tempStr = substr($tempStr, 0, strrpos($tempStr, ".", 0));
            $i = $tempStr * $listPageNumber + 1;
        }
        $listPageNumber = $i + $listPageNumber - 1;
        if ($listPageNumber > $totalPageNumber)
            $listPageNumber = $totalPageNumber;
        if (($listPageNumber - $i) < $temp)
            $i = $listPageNumber - $temp + 1;
        if ($i <= 0)
            $i = 1;
        for (; $i <= $listPageNumber; $i++)
        {
            $navigatorStr = $navigatorStr . " <span ";
            if ($i == $currentPageNumber)
                $navigatorStr = $navigatorStr . "class=\"now02\">" . $i;
            else
                $navigatorStr = $navigatorStr . ">" . link_to($i, $url . $i);
            //"<a href=\"".$url.$i."\">".$i."</a>";
            $navigatorStr = $navigatorStr . "</span>";
        }
        if ($currentPageNumber < $totalPageNumber)
            $navigatorStr = $navigatorStr . " <span>" . link_to("下一页", $url . ($currentPageNumber + 1)) . "</span>"; //<a href=\"".$url.($currentPageNumber+1)."\">下一页</a></span>";
        else
            $navigatorStr = $navigatorStr . " <span style=\"color:#d0d0d0\">下一页</span>";

        $navigatorStr = $navigatorStr . "</div>";
        return $navigatorStr;
    }

    public static function DateDiff($date1, $date2)
    { //时间比较函数，返回两个日期相差几秒、几分钟、几小时或几天(当前时间，比较时间)
        $time1 = strtotime($date1);
        $time2 = strtotime($date2);
        $diff = 0;
        $result = "";
        if ($time1 && $time2) {
            $diff = (float)($time1 - $time2); //秒
            if ($diff >= 60) {
                $diff = (float)($time1 - $time2) / 60; //分
                if ($diff >= 60) {
                    $diff = (float)($time1 - $time2) / 3600; //时
                    if ($diff >= 24) {
                        $diff = (float)($time1 - $time2) / 86400; //天
                        if ($diff >= 30) {
                            $diff = (float)($time1 - $time2) / 2592000; //月
                            if ($diff >= 12) {
                                $diff = (float)($time1 - $time2) / 31536000; //年
                                $result = (integer)$diff . "年前";
                            } else
                                $result = (integer)$diff . "月前";
                        } else
                            $result = (integer)$diff . "天前";
                    } else
                        $result = (integer)$diff . "小时前";
                } else
                    $result = (integer)$diff . "分钟前";
            } else if ($diff >= 1)
                $result = $diff . "秒前";
            else
                $result = "刚刚";
            return $result;
        }
    }

    public static function getGentleDateTime($date)
    {
        return utility::DateDiff(date("Y-m-d H:i:s"), $date);
    }

    public static function getNoHtmlStr($resource, $startIndex = 0, $length, $more = "")
    {
        //$resource = preg_replace("/<[^>]*>/i","",$resource);
        //$resource = preg_replace(array("/<script.*>.*<\/script>/siU","\r\n"),array("",""),$resource);
        $resource = preg_replace("/<script.*>.*<\/script>/siU", "", $resource);
        $resource = self::getNoBlankStr(trim(strip_tags($resource)));
        /* 该编码每个非英文字符的字节长度 */
        //$encode_len = $encode == 'utf-8' ? 3 : 2;
        $encode_len = 3;
        /* 计算开始字节 */
        for ($byteStart = $i = 0; $i < $startIndex; ++$i) {
            $byteStart += ord($resource{$byteStart}) < 128 ? 1 : $encode_len;
            /* 当起始坐标超出字符串，则返回空值 */
            if ($resource{$byteStart} == '') return '';
        }
        /* 计算字节长度 */
        for ($i = 0, $byteLen = $byteStart; $i < $length; ++$i)
            $byteLen += ord($resource{$byteLen}) < 128 ? 1 : $encode_len;
        $tempStr = substr($resource, $byteStart, $byteLen - $byteStart);
        if (strlen($resource) > strlen($tempStr))
            $tempStr = $tempStr . $more;
        return $tempStr;
        //        $tempStr = mb_substr($resource,$startIndex,$length,'UTF-8');
        //        if(strlen($resource) > strlen($tempStr))
        //            $tempStr = $tempStr."...";
        //        return $tempStr;
    }

    public static function getNoBlankStr($sourceStr)
    {
        $sourceStr = trim($sourceStr);
        $pattern = "/^((&nbsp;)+( )*\s*)*|\s/";
        return preg_replace($pattern, "", $sourceStr);
    }


    public static function getRequestIp($request)
    {
        $ip = $request->getHttpHeader('addr', 'remote');
        return $ip;
    }


    public static function renderCelebrityLinkIfExist($name)
    {
        $celebrity = @Doctrine_Core::getTable('celebrity')->createQuery('c')
                ->addWhere('c.name=?', trim($name))->fetchOne();
        if ($celebrity != null) {
            return link_to($name, 'celebrity/detail?celebrityId=' . $celebrity->getCelebrityId(), "target='_blank'");
        }
        return $name;
    }

//    获取明星的小头像,如果没有则使用大头像
    public static function getCelebrityImgSmallElseBig(Celebrity $celebrity)
    {
        return $celebrity->getSnapshotSmall() != null ? $celebrity->getSnapshotSmall() : $celebrity->getSnapshotBig();
    }

    public static  function isNotNullOrEmpty($var)
    {
        if ($var == null) return false;
        if (trim($var) == "") return false;
        return true;
    }

    
}

?>